﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using System.Collections.Specialized;
using Microsoft.Practices.Unity;
namespace Infrastructure.WpfClient.ExceptionPolicy
{
    [ConfigurationElementType(typeof(CustomHandlerData))]
    public class ExceptionBoxHandler : IExceptionHandler
    {
        public ExceptionBoxHandler(NameValueCollection args)
        {

        }


        #region IExceptionHandler Members

        public Exception HandleException(Exception exception, Guid handlingInstanceId)
        {
            var container = Infrastructure.Aspect.Patterns.Singleton<UnityContainer, IUnityContainer>.Instance;
            if (exception != null && exception is System.Data.SqlClient.SqlException && ((System.Data.SqlClient.SqlException)exception).Number == 547)
            {
                exception = new Exception("This item cannot be deleted as it is in use (it is related to another entity in the database).");
            }
            var view = container.Resolve<Views.ExceptionBox.ExceptionBox>();
            view.CurrentException = exception;
            view.ShowDialog();            
            return exception;
        }

        #endregion
    }
}
